// 模拟合同变更数据
const contractChanges = [
  {
    id: 1,
    customerCode: 'CU001',
    customerName: '北京科技有限公司',
    companyType: '有限责任公司',
    contractMonthlyFee: 5000,
    newContractMonthlyFee: 4800,
    area: '北京',
    monthlyFee: 4800,
    newMonthlyFee: 4500,
    contractStatus: '变更中',
    changeReason: '业务范围调整，需要减少部分服务项目',
    changeDescription: '取消税务筹划服务，保留基础代账服务',
    createTime: '2024-01-15',
    approveTime: null,
  },
  {
    id: 2,
    customerCode: 'CU002',
    customerName: '上海贸易股份公司',
    companyType: '股份有限公司',
    contractMonthlyFee: 8000,
    newContractMonthlyFee: 7500,
    area: '上海',
    monthlyFee: 7500,
    newMonthlyFee: 7200,
    contractStatus: '变更中',
    changeReason: '公司规模扩大，需要增加服务范围',
    changeDescription: '新增财务咨询和内控制度建设服务',
    createTime: '2024-01-20',
    approveTime: null,
  },
  {
    id: 3,
    customerCode: 'CU003',
    customerName: '深圳创新企业',
    companyType: '有限责任公司',
    contractMonthlyFee: 6000,
    newContractMonthlyFee: 5800,
    area: '深圳',
    monthlyFee: 5800,
    newMonthlyFee: 5600,
    contractStatus: '已变更',
    changeReason: '合同到期续签，调整收费标准',
    changeDescription: '续签一年合同，月收费从6000元调整为5800元',
    createTime: '2024-01-10',
    approveTime: '2024-01-12',
  },
  {
    id: 4,
    customerCode: 'CU004',
    customerName: '广州制造有限公司',
    companyType: '有限责任公司',
    contractMonthlyFee: 7200,
    newContractMonthlyFee: 6800,
    area: '广州',
    monthlyFee: 6800,
    newMonthlyFee: 6500,
    contractStatus: '已变更',
    changeReason: '业务量减少，需要调整服务内容',
    changeDescription: '减少报表分析频次，由月度改为季度',
    createTime: '2024-01-05',
    approveTime: '2024-01-08',
  },
  {
    id: 5,
    customerCode: 'CU005',
    customerName: '杭州电商企业',
    companyType: '有限责任公司',
    contractMonthlyFee: 4500,
    newContractMonthlyFee: 5200,
    area: '杭州',
    monthlyFee: 4800,
    newMonthlyFee: 5200,
    contractStatus: '变更中',
    changeReason: '电商业务增长，需要增加税务申报服务',
    changeDescription: '新增增值税专用发票开具及管理服务',
    createTime: '2024-01-25',
    approveTime: null,
  },
];

let nextId = 6;

export default {
  // 获取合同变更列表
  'GET /api/contract-changes': (req, res) => {
    const { status, current = 1, pageSize = 10, customerCode, customerName, companyType, area, contractStatus } = req.query;
    
    let filteredData = contractChanges.filter(item => {
      // 根据Tab页筛选
      if (status === '待变更' && item.contractStatus !== '变更中') return false;
      if (status === '已变更' && item.contractStatus !== '已变更') return false;
      
      // 根据筛选条件过滤
      if (customerCode && !item.customerCode.includes(customerCode)) return false;
      if (customerName && !item.customerName.includes(customerName)) return false;
      if (companyType && item.companyType !== companyType) return false;
      if (area && item.area !== area) return false;
      if (contractStatus && item.contractStatus !== contractStatus) return false;
      
      return true;
    });

    const total = filteredData.length;
    const start = (current - 1) * pageSize;
    const end = start + parseInt(pageSize);
    const data = filteredData.slice(start, end);

    setTimeout(() => {
      res.json({
        success: true,
        data: {
          list: data,
          total,
          current: parseInt(current),
          pageSize: parseInt(pageSize),
        },
      });
    }, 300);
  },

  // 创建合同变更
  'POST /api/contract-changes': (req, res) => {
    const { contractId, ...otherData } = req.body;
    
    const newChange = {
      id: nextId++,
      ...otherData,
      contractId, // 关联的原合同ID
      contractStatus: '变更中',
      createTime: new Date().toISOString().split('T')[0],
      approveTime: null,
    };
    
    contractChanges.unshift(newChange);
    
    setTimeout(() => {
      res.json({
        success: true,
        data: newChange,
        message: '创建成功',
      });
    }, 300);
  },

  // 更新合同变更
  'PUT /api/contract-changes/:id': (req, res) => {
    const { id } = req.params;
    const index = contractChanges.findIndex(item => item.id == id);
    
    if (index > -1) {
      contractChanges[index] = {
        ...contractChanges[index],
        ...req.body,
      };
      
      setTimeout(() => {
        res.json({
          success: true,
          data: contractChanges[index],
          message: '更新成功',
        });
      }, 300);
    } else {
      res.status(404).json({
        success: false,
        message: '记录不存在',
      });
    }
  },

  // 删除合同变更
  'DELETE /api/contract-changes/:id': (req, res) => {
    const { id } = req.params;
    const index = contractChanges.findIndex(item => item.id == id);
    
    if (index > -1) {
      contractChanges.splice(index, 1);
      
      setTimeout(() => {
        res.json({
          success: true,
          message: '删除成功',
        });
      }, 300);
    } else {
      res.status(404).json({
        success: false,
        message: '记录不存在',
      });
    }
  },

  // 审批通过合同变更
  'POST /api/contract-changes/:id/approve': (req, res) => {
    const { id } = req.params;
    const index = contractChanges.findIndex(item => item.id == id);
    
    if (index > -1) {
      contractChanges[index] = {
        ...contractChanges[index],
        contractStatus: '已变更',
        approveTime: new Date().toISOString().split('T')[0],
      };
      
      setTimeout(() => {
        res.json({
          success: true,
          data: contractChanges[index],
          message: '变更成功',
        });
      }, 300);
    } else {
      res.status(404).json({
        success: false,
        message: '记录不存在',
      });
    }
  },

  // 拒绝合同变更
  'POST /api/contract-changes/:id/reject': (req, res) => {
    const { id } = req.params;
    const { reason } = req.body;
    const index = contractChanges.findIndex(item => item.id == id);
    
    if (index > -1) {
      contractChanges[index] = {
        ...contractChanges[index],
        contractStatus: '已拒绝',
        rejectReason: reason,
        rejectTime: new Date().toISOString().split('T')[0],
      };
      
      setTimeout(() => {
        res.json({
          success: true,
          data: contractChanges[index],
          message: '拒绝成功',
        });
      }, 300);
    } else {
      res.status(404).json({
        success: false,
        message: '记录不存在',
      });
    }
  },
}; 